<?php
/*
 *  Copyright (c) 2024 Borlabs GmbH. All rights reserved.
 *  This file may not be redistributed in whole or significant part.
 *  Content of this file is protected by international copyright laws.
 *
 *  ----------------- Borlabs Cookie IS NOT FREE SOFTWARE -----------------
 *
 *  @copyright Borlabs GmbH, https://borlabs.io
 */

declare(strict_types=1);

namespace Borlabs\Cookie\System\WordPressAdminDriver;

use Borlabs\Cookie\Adapter\WpFunction;
use Borlabs\Cookie\Controller\Admin\CloudScan\CloudScanController;
use Borlabs\Cookie\Controller\Admin\CompatibilityPatches\CompatibilityPatchesController;
use Borlabs\Cookie\Controller\Admin\ConsentLog\ConsentLogController;
use Borlabs\Cookie\Controller\Admin\ContentBlocker\ContentBlockerAppearanceController;
use Borlabs\Cookie\Controller\Admin\ContentBlocker\ContentBlockerController;
use Borlabs\Cookie\Controller\Admin\ContentBlocker\ContentBlockerSettingsController;
use Borlabs\Cookie\Controller\Admin\Dashboard\DashboardController;
use Borlabs\Cookie\Controller\Admin\Debug\DebugController;
use Borlabs\Cookie\Controller\Admin\Dialog\DialogAppearanceController;
use Borlabs\Cookie\Controller\Admin\Dialog\DialogLocalizationController;
use Borlabs\Cookie\Controller\Admin\Dialog\DialogSettingsController;
use Borlabs\Cookie\Controller\Admin\IabTcf\IabTcfSettingsController;
use Borlabs\Cookie\Controller\Admin\IabTcf\IabTcfVendorController;
use Borlabs\Cookie\Controller\Admin\ImportExport\ImportExportController;
use Borlabs\Cookie\Controller\Admin\Library\LibraryController;
use Borlabs\Cookie\Controller\Admin\License\LicenseController;
use Borlabs\Cookie\Controller\Admin\LocalizationChecker\LocalizationCheckerController;
use Borlabs\Cookie\Controller\Admin\Log\LogController;
use Borlabs\Cookie\Controller\Admin\Provider\ProviderController;
use Borlabs\Cookie\Controller\Admin\ScriptBlocker\ScriptBlockerController;
use Borlabs\Cookie\Controller\Admin\Service\ServiceController;
use Borlabs\Cookie\Controller\Admin\ServiceGroup\ServiceGroupController;
use Borlabs\Cookie\Controller\Admin\Settings\SettingsController;
use Borlabs\Cookie\Controller\Admin\StyleBlocker\StyleBlockerController;
use Borlabs\Cookie\Controller\Admin\Widget\WidgetController;
use Borlabs\Cookie\Localization\System\WordPressPageManagerLocalizationStrings;
use Borlabs\Cookie\System\Package\PackageManager;

final class WordPressPageManager
{
    private const CAPABILITY = 'manage_borlabs_cookie';

    private string $mainMenuSlug;

    private PackageManager $packageManager;

    private WordPressControllerBridge $wordPressControllerBridge;

    private WordPressPageManagerLocalizationStrings $wordPressPageManagerLocalizationStrings;

    private WpFunction $wpFunction;

    public function __construct(
        PackageManager $packageManager,
        WordPressControllerBridge $wordPressControllerBridge,
        WordPressPageManagerLocalizationStrings $wordPressPageManagerLocalizationStrings,
        WpFunction $wpFunction
    ) {
        $this->packageManager = $packageManager;
        $this->wordPressControllerBridge = $wordPressControllerBridge;
        $this->wordPressPageManagerLocalizationStrings = $wordPressPageManagerLocalizationStrings;
        $this->wpFunction = $wpFunction;
        $this->mainMenuSlug = DashboardController::CONTROLLER_ID;
    }

    public function register(): void
    {
        $this->registerBorlabsCookieMenu();
        $this->registerSubMenuDashboard();
        $this->registerSubMenuSettings();
        $this->registerSubMenuDialog();
        $this->registerSubMenuWidget();
        $this->registerSubMenuConsentManagement();
        $this->registerSubMenuContentBlocker();
        $this->registerSubMenuScriptBlocker();
        $this->registerSubMenuStyleBlocker();
        $this->registerSubMenuScanner();
        $this->registerSubMenuLibrary();
        $this->registerSubMenuSystem();

        if (defined('BORLABS_COOKIE_DEV_MODE_ENABLE_DEBUG_INTERFACE') && constant('BORLABS_COOKIE_DEV_MODE_ENABLE_DEBUG_INTERFACE')) {
            $this->registerSubMenuDebug();
        }

        if (defined('BORLABS_COOKIE_DEV_MODE_ENABLE_LOCALIZATION_CHECKER_INTERFACE') && constant('BORLABS_COOKIE_DEV_MODE_ENABLE_LOCALIZATION_CHECKER_INTERFACE')) {
            $this->registerSubMenuLocalizationChecker();
        }
    }

    private function isBorlabsCookiePage(): bool
    {
        return isset($_GET['page']) && strpos($_GET['page'], 'borlabs-cookie') !== false;
    }

    private function packageUpdateCount(): string
    {
        $count = $this->packageManager->getPackageUpdateCount();

        if ($count === 0) {
            return '';
        }

        return '<span
            class="menu-counter"
            style="display: inline-block; vertical-align: top; box-sizing: border-box; padding: 0 5px; min-width: 18px; height: 18px; border-radius: 9px; font-size: 11px; line-height: 1.6; text-align: center; z-index: 26; background: #9333ea; color: #fff; letter-spacing: 0; margin: 0 0 0 2px">'
            . $count
            . '</span>';
    }

    private function registerBorlabsCookieMenu(): void
    {
        $menuTitle = $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['borlabsCookie'];
        $packageUpdateCount = $this->packageUpdateCount();

        if (!$this->isBorlabsCookiePage() && $packageUpdateCount !== '') {
            $menuTitle = '<span style="letter-spacing: -1px;">' . $menuTitle . '</span> ' . $packageUpdateCount;
        }

        $this->wpFunction->addMenuPage(
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['borlabsCookie'],
            $menuTitle,
            self::CAPABILITY,
            DashboardController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DashboardController::class],
            $this->svgIcon(),
        );
    }

    private function registerSubMenuConsentManagement(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['consentManagement'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['consentManagement'],
            self::CAPABILITY,
            ServiceController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ServiceController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['services'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['services'],
            self::CAPABILITY,
            ServiceController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ServiceController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['serviceGroups'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['serviceGroups'],
            self::CAPABILITY,
            ServiceGroupController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ServiceGroupController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['iabTcf'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['iabTcf'],
            self::CAPABILITY,
            IabTcfVendorController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, IabTcfVendorController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['iabTcfManageVendors'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['iabTcfManageVendors'],
            self::CAPABILITY,
            IabTcfVendorController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, IabTcfVendorController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['iabTcfSettings'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['iabTcfSettings'],
            self::CAPABILITY,
            IabTcfSettingsController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, IabTcfSettingsController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['providers'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['providers'],
            self::CAPABILITY,
            ProviderController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ProviderController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['consentLogs'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['consentLogs'],
            self::CAPABILITY,
            ConsentLogController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ConsentLogController::class],
        );
    }

    private function registerSubMenuContentBlocker(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['contentBlockers'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['contentBlockers'],
            self::CAPABILITY,
            ContentBlockerController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ContentBlockerController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['contentBlockersManage'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['contentBlockersManage'],
            self::CAPABILITY,
            ContentBlockerController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ContentBlockerController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['contentBlockersSettings'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['contentBlockersSettings'],
            self::CAPABILITY,
            ContentBlockerSettingsController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ContentBlockerSettingsController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['contentBlockersAppearance'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['contentBlockersAppearance'],
            self::CAPABILITY,
            ContentBlockerAppearanceController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ContentBlockerAppearanceController::class],
        );
    }

    private function registerSubMenuDashboard(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['dashboard'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['dashboard'],
            self::CAPABILITY,
            DashboardController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DashboardController::class],
        );
    }

    private function registerSubMenuDebug(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            'Debug',
            'Debug',
            self::CAPABILITY,
            DebugController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DebugController::class],
        );
    }

    private function registerSubMenuDialog(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['dialog'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['dialog'],
            self::CAPABILITY,
            DialogSettingsController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DialogSettingsController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['dialogSettings'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['dialogSettings'],
            self::CAPABILITY,
            DialogSettingsController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DialogSettingsController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['dialogAppearance'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['dialogAppearance'],
            self::CAPABILITY,
            DialogAppearanceController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DialogAppearanceController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['dialogLocalization'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['dialogLocalization'],
            self::CAPABILITY,
            DialogLocalizationController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DialogLocalizationController::class],
        );
    }

    private function registerSubMenuLibrary(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['library'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['library'],
            self::CAPABILITY,
            LibraryController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, LibraryController::class],
        );
    }

    private function registerSubMenuLocalizationChecker(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            'Localization Checker',
            'Localization Checker',
            self::CAPABILITY,
            LocalizationCheckerController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, LocalizationCheckerController::class],
        );
    }

    private function registerSubMenuScanner(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['scanner'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['scanner'],
            self::CAPABILITY,
            CloudScanController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, CloudScanController::class],
        );
    }

    private function registerSubMenuScriptBlocker(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['scriptBlockers'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['scriptBlockers'],
            self::CAPABILITY,
            ScriptBlockerController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ScriptBlockerController::class],
        );
    }

    private function registerSubMenuSettings(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['settings'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['settings'],
            self::CAPABILITY,
            SettingsController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, SettingsController::class],
        );
    }

    private function registerSubMenuStyleBlocker(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['styleBlockers'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['styleBlockers'],
            self::CAPABILITY,
            StyleBlockerController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, StyleBlockerController::class],
        );
    }

    private function registerSubMenuSystem(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['system'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['system'],
            self::CAPABILITY,
            DashboardController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, DashboardController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['compatibilityPatches'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['compatibilityPatches'],
            self::CAPABILITY,
            CompatibilityPatchesController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, CompatibilityPatchesController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['importExport'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['importExport'],
            self::CAPABILITY,
            ImportExportController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, ImportExportController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['license'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['license'],
            self::CAPABILITY,
            LicenseController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, LicenseController::class],
        );
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['logs'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['logs'],
            self::CAPABILITY,
            LogController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, LogController::class],
        );
    }

    private function registerSubMenuWidget(): void
    {
        $this->wpFunction->addSubMenuPage(
            $this->mainMenuSlug,
            $this->wordPressPageManagerLocalizationStrings->get()['siteTitle']['widget'],
            $this->wordPressPageManagerLocalizationStrings->get()['menuTitle']['widget'],
            self::CAPABILITY,
            WidgetController::CONTROLLER_ID,
            [$this->wordPressControllerBridge, WidgetController::class],
        );
    }

    private function svgIcon(): string
    {
        return 'data:image/svg+xml;base64, ';
    }
}
